๐Ÿ“ฆ calda / Word-World-Friends

๐Ÿ“„ FriendsViewController.swift ยท 137 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137//
//  FriendsViewController.swift
//  WordWorld
//
//  Created by Cal on 6/18/15.
//  Copyright ยฉ 2015 Hear a Tale. All rights reserved.
//

import Foundation
import UIKit

let WWFriendsSettingsPaths = ["you", "friend1", "friend2", "friend3", "friend4"]
var WWFriendsSize: CGSize!
let WWUpdateFriendsNotification = "com.hearatale.wordworld.update-friends-notification"

class FriendsViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        NotificationCenter.default.addObserver(self, selector: #selector(FriendsViewController.updateFriends), name: NSNotification.Name(rawValue: WWUpdateFriendsNotification), object: nil)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "body", for: indexPath) as! FriendCell
        cell.decorate(indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let height = collectionView.frame.height
        let width = height * 0.53
        return CGSize(width: width, height: height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0.0
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let bodyEditController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "bodyEdit") as! BodyViewController
        bodyEditController.prepareEditor(color: friendColorForIndexPath(indexPath), friend: WWFriendsSettingsPaths[indexPath.item])
        self.present(bodyEditController, animated: true, completion: nil)
    }
    
    @objc func updateFriends() {
        collectionView.reloadData()
    }
    
    @IBAction func dismiss(_ sender: AnyObject) {
        self.dismiss(animated: true, completion: nil)
    }
}

class FriendCell : UICollectionViewCell {
    
    @IBOutlet weak var image: UIImageView!
    @IBOutlet weak var colorBackground: UIView!
    @IBOutlet weak var text: UILabel!
    
    func decorate(_ index: IndexPath) {
        
        WWFriendsSize = CGSize(width: image.frame.width * UIScreen.main.scale, height: image.frame.height * UIScreen.main.scale)
        
        let title = (index.item == 0 ? "You" : "Friend \(index.item)")
        text.text = title
        
        let color = friendColorForIndexPath(index)
        colorBackground.backgroundColor = color
        
        //load friend image if settings path exists
        let userData = UserDefaults.standard
        if userData.value(forKey: WWFriendsSettingsPaths[index.item]) != nil {
            
            let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentsPath = paths[0]
            let savePath = (documentsPath as NSString).appendingPathComponent("\(WWFriendsSettingsPaths[index.item]).png")
            
            let data = try? Data(contentsOf: URL(fileURLWithPath: savePath))
            if let data = data {
                let image = UIImage(data: data)
                self.image.image = image
            }
            
        }
        
        else {
            print("\(index.item) to blank")
            self.image.image = UIImage(named: "Body Feature/Body-Feature-body.png")
        }
        
    }
    
}

//pragma MARK: - UIColor.. stuff..

extension UIColor {
    
    func darken() -> UIColor {
        var hue: CGFloat = 0.0
        var sat: CGFloat = 0.0
        var bright: CGFloat = 0.0
        self.getHue(&hue, saturation: &sat, brightness: &bright, alpha: nil)
        
        return UIColor(hue: hue, saturation: sat - 0.1, brightness: bright - 0.1, alpha: 1.0)
    }
    
    func lighten() -> UIColor {
        var hue: CGFloat = 0.0
        var sat: CGFloat = 0.0
        var bright: CGFloat = 0.0
        self.getHue(&hue, saturation: &sat, brightness: &bright, alpha: nil)
        
        return UIColor(hue: hue, saturation: sat + 0.1, brightness: bright + 0.1, alpha: 1.0)
    }
    
}

func friendColorForIndexPath(_ indexPath: IndexPath) -> UIColor {
    let index = indexPath.item
    
    var hue = CGFloat(202.0 / 360.0)
    hue += 0.11 * CGFloat(index)
    if hue > 1.0 { hue -= 1.0 }
    
    return UIColor(hue: hue, saturation: 0.52, brightness: 0.84, alpha: 1.0)
}